/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.babeuf.form.conditions
{
	import net.babeuf.form.conditions.AbstractCondition;
	import net.babeuf.form.fields.AbstractField;
	
	/**
	 * The AtLeastOneFilledCondition class is used to check that in the passed in fields, one at least is filled
	 * 
	 * @example
	 * <listing>
	 * var f1 : InputField = new InputField( field1, "field1") ; 
	 * var f2 : InputField = new InputField( field2, "field2") ; 
	 * var f3 : InputField = new InputField( field3, "field3") ;  
	 * 
	 * var condition : AtLeastOneFilledCondition = new AtLeastOneFilledCondition(f1, f2, f3) ; 
	 * trace(condition.match()) ; // false
	 * 
	 * f2.setValue("random value") ; 
	 * 
	 * trace(condition.match()) ; // true 
	 * </listing> 
	 * 
	 * @langversion 3.0
     * @playerversion Flash 10
	 * 
	 * @author damien pegoraro
	 */
	public class AtLeastOneFilledCondition extends AbstractCondition implements ICondition
	{
		/**
		 * @private
		 */
		private var fields : Array ;
		
		/**
		 * Creates a new instance
		 * 
		 * @param	...fields the <code>AbstractFields</code> that must be checked by the condition
		 * 
		 * @see net.babeuf.form.fields.AbstractField
		 * 
		 * @langversion 3.0
		 * @playerversion Flash 10
		 */
		public function AtLeastOneFilledCondition(...fields ) 
		{			
			this.fields = fields ; 
		}
		
		/**
		 * Add an extra field to check to the condition, this field will be added to the current ones without erasing them
		 * @param	field the <code>AbstractField</code> to check
		 * 
		 * @langversion 3.0
		 * @playerversion Flash 10
		 */
		public function addField( field:  AbstractField ) : void
		{			
			if (!fields) fields = new Array() ; 
			fields.push(field) ; 
		}
		
		/**
		 * @private
		 */	
		public function reset() : void
		{
			_updateSensors(true) ;
		}
		
		/**
		 * @inheritDoc
		 */
		override public function match():Boolean 
		{
			
			super.match() ; 
			var ok : Boolean = false ; 
			
			for each ( var a : AbstractField in fields ) 
			{
				if (  a.getValue() != "" ) 
				{
					ok = true ; 
					break ; 
				}
			}	
			
			if ( !ok ) { _updateSensors(false) ; return false ; }
			return true ; 
		}
		
		/**
		 * @inheritDoc
		 */
		override public function dispose():void 
		{
			super.dispose();
			fields = null ; 
		}
	}

}